<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="/js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <style type="text/css">
        #login{
            width:400px;
            height:280px;
            position:absolute;
            left: 50%;
            top: 50%;
            margin-left:-200px;
            margin-top:-140px;
            border:1px;
            align:center;
        }
        #app{
            width:300px;
            height:160px;
            position:relative;
            left:50%;
            top:50%;
            margin-left:-150px;
            margin-top:-80px;
        }
    </style>
</head>
<body>
<script type="text/javascript" src="/js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<div class="el-container">
    <img src="/img/bg.jpg">
</div>
    <form id="login">
        <div id="app">
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

                <el-form-item label="用户名" prop="username">
                    <el-input v-model.String="ruleForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="pass">
                    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </form>
</body>
<link rel="stylesheet" href="../plugins/elementui/index.css">
<script src="../js/axios-0.18.0.js"></script>
<script type="text/javascript">
    var Main = {
        data() {
            var checkAge = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('用户名不能为空'));
                }
                setTimeout(() => {
                        if (value==null) {
                            callback(new Error('必须填写用户名'));
                        } else {
                            callback();
                        }

                }, 1000);
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    pass: '',
                    checkPass: '',
                    username: ''
                },
                rules: {
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    username: [
                        { validator: checkAge, trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        var pass=this.ruleForm.pass;
                        var name=this.ruleForm.username;
                        var param={pass,name};
                        axios.post("http://localhost:82/user/findByName.do",this.ruleForm.username).then((response)=>{
                            /*alert(this.ruleForm.pass);*/
                            console.log(response);
                            if(response.data.data>0){
                                axios.post("http://localhost:82/user/update.do",param).then((response)=>{
                                    this.$message({
                                        message:response.data.message,
                                        type:response.data.flag?"success":"error"

                                      });
                                    if(this.$message){
                                        this.$alert(
                                            '<a href="http://localhost:83/login.html">请重新登录</a>',
                                            {
                                                dangerouslyUseHTMLString: true,
                                                center:true,
                                                showClose:false,
                                            }
                                        ).then((res)=>{
                                            document.location.href="http://localhost:83/login.html"
                                        });
                                    }
                                }).catch((err)=>{
                                    console.log(err)
                                })
                            }else{
                                this.$message({
                                    message:response.data.message,
                                    type:response.data.flag?"success":"error"
                                })
                            }




                        }).catch((err)=>{
                            console.log(err);
                        });


                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
    var Ctor = Vue.extend(Main)
    new Ctor().$mount('#app')
</script>
</html>